package s.k.gui.webtest.element;

import s.k.gui.webtest.Allocator;
import s.k.gui.webtest.BrowserWindow;
import s.k.gui.webtest.WebDriverBrowserWindow;

public class TextField extends Element {

	public TextField(String comment) {

		super(comment);

	}

	public void setText(String text) {

		StringBuilder message = new StringBuilder();

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (isExist()) {

			message.append(this.comment + ":");

			if (currentWindow instanceof WebDriverBrowserWindow) {

				element.sendKeys(text);

				message.append("输入成功");

				logger.info(message);
			}

		} else {

			message.append(this.comment + ":");

			message.append("设置值"+ text +"失败!找不到元素");

			logger.error(message);

		}

	}

	/**这个要覆盖父类的方法，获得此输入框的内容
	 * @return 输入框的内容，字符窜信息*/
	@Override
	public String getText() {

		StringBuilder message = new StringBuilder();

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		String content = null;

		if (isExist()) {

			message.append(this.comment + ":");

			if (currentWindow instanceof WebDriverBrowserWindow) {

				content = element.getAttribute("value");
			}

			message.append("获得输入框文本内容成功!");

			logger.info(message);

		} else {

			message.append(this.comment + ":");

			message.append("获得输入框文本内容失败!找不到元素");

			logger.error(message);

		}

		return content;
	}


	public void clear() {

		StringBuilder message = new StringBuilder();

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (isExist()) {

			message.append(this.comment + ":");

			if (currentWindow instanceof WebDriverBrowserWindow) {

				element.clear();

			}

			message.append("输入框的值清空成功!");

			logger.info(message);

		} else {

			message.append(this.comment + ":");

			message.append("输入框的值清空失败!找不到元素");

			logger.error(message);
		}
	}
}
